package com.team_project.boot.controller;

import com.sun.deploy.net.HttpResponse;
import com.sun.imageio.spi.OutputStreamImageOutputStreamSpi;
import com.team_project.boot.entity.User;
import com.team_project.boot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper userMapper;

    @GetMapping("/check")
    public String reg(String username){
        System.out.println("username"+username);
        String oldName = userMapper.selectUserName(username);
        System.out.println(oldName);
        if (username.equals(oldName)){
            return "用户名已存在";
        }
        return "用户名可用";
    }
    @PostMapping("/reg")
    public int reg(User user){
        user.setGender("男");
        user.setRole(1);
        System.out.println(user);
        userMapper.insert(user);
        return 1;
    }
    @PostMapping("/login")
    public int login(User user, HttpSession session,String rem , HttpServletResponse response){
        System.out.println(user.getUsername()+""+user.getPassword());
//        String pw = userMapper.selectUserPw(username);
         User user1 = userMapper.selectUser(user.getUsername());
        if (user1!=null){
            if (user1.getPassword().equals(user.getPassword())){
                session.setAttribute("user", user1);
                if (rem!=null){
                    Cookie cookie = new Cookie("username", user1.getUsername());
                    Cookie cookie1 = new Cookie("password", user1.getPassword());
                    response.addCookie(cookie);
                    response.addCookie(cookie1);
                    cookie.setMaxAge(60*60*24*30);//保存时间
                    cookie1.setMaxAge(60*60*24*30);//保存时间
                }
                return 1;
            }
            else
                return 2;
        }
        return 3;
    }
    @RequestMapping("/currentUser")
    public Boolean currentUser(HttpSession session){
        return session.getAttribute("user")==null?false:true;
    }
    @RequestMapping("/loginout")
    public void loginout(HttpSession session){
        session.removeAttribute("user");
    }
    @RequestMapping("/findUserAll")
    public List<User> findUserAll(){
        return userMapper.selectUserAll();
    }

    @RequestMapping("/deletebyid")
    public void deletebyid(Integer id){
        userMapper.deleteById(id);
    }

    @RequestMapping("/updateById")
    public void updateById(User user){
        userMapper.updateById(user);
    }

    @RequestMapping("/selectbyIdMember")
    public User selectbyIdMember(Integer id){
        return userMapper.selectbyIdMember(id);
    }
}
